<?php

Ph::import("library.sm");

class AdminsModule extends Module
{
	public $admin = NULL;
	
	public function __config()
	{
		$this->page->template = "control/index.tpl";
		$this->locale->add("control.ini");
		if ( !$this->site->admin_is_auth() )
		{
			$this->site->admin_clear_auth();
			header('Location: /panel/login');
		}
		
		$this->helper = new SMHelper( $this->site );
		
		$this->admin = $this->helper->defaults();
		if ( !$this->admin->access("admins") ) $this->__stop();
	}
	
	
	public function __default()
	{
		$this->helper->setMenuActive("admins");
		$this->page->title("_titleAdmins", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		
		$this->helper->addBodyTitleButton("/panel/admins/add", "_adminsCreate", "blue");
		$this->page->loadTplToVar("content", "admins/index.tpl");
		
	}
	
	public function AddAction()
	{
		$this->helper->setMenuActive("admins");
		$this->helper->setSubmenuActive('add');
		$this->page->title("_titleAdminsAdd", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		
		$this->helper->addBodyTitleButton("/panel/admins/", "_Back", "white");
		$this->page->loadTplToVar('content', 'admins/create.tpl');
		
		// List access point for domains
		$domains = ORM::for_table('store')->find_array();
		$this->page->setVar('all_domains', $domains);
		$this->page->setVar('BaseSiteDomain', getHost());
		
		unset(
			$_SESSION['name'],
			$_SESSION['email'],
			$_SESSION['password'],
			$_SESSION['access'],
			$_SESSION['level']
		);
	}
	
	public function AbortAction()
	{
		unset(
			$_SESSION['name'],
			$_SESSION['email'],
			$_SESSION['password'],
			$_SESSION['access'],
			$_SESSION['level']
		);
		
		header("Location: /panel/admins/add");
	}
	
	public function ConfirmAction()
	{
		$this->helper->setMenuActive("admins");
		$this->page->title("_titleAdminsAdd", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		
		$this->helper->addBodyTitleButton("/panel/admins/abort", "_Back", "white");
		$this->page->loadTplToVar('content', 'admins/affirm.tpl');
		$this->page->setVar('content', false);
		$this->page->setVar('errors');
		
		$name = $this->site->_post('name');
		$email = $this->site->_post('email');
		$password = $this->site->_post('password');
		$access = $this->site->_post('access');
		$level = intval( $this->site->_post('level') );
		
		$access_array = explode(',', str_replace(' ', '', $access));
		
		// -------------------------------------------------------------
		
		$errors = false;
		
		if ( strlen($name) < 3 )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_adminsSmallNameLength");
			$this->page->append("errors", $error->processing());
			$errors = true;
		}
		
		if ( strlen($email) < 5 )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_adminsNotEmail");
			$this->page->append("errors", $error->processing());
			$errors = true;
		}
		
		if ( strlen($password) < 8 )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_adminsSmallPassword");
			$this->page->append("errors", $error->processing());
			$errors = true;
		}
		
		if ( array_search( 'root', $access_array ) !== false )
		{
			$error = new Template("stores/small_notice.tpl");
			$error->setLocVar("noticeText", "_text_adminsAddingRoot");
			$this->page->append("errors", $error->processing());
		}
		
		if ( strlen($password) > 26 )
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_adminsBigPassword");
			$this->page->append("errors", $error->processing());
			$errors = true;
		}
		
		if ( strlen($access) < 1 )
		{
			$error = new Template("stores/small_notice.tpl");
			$error->setLocVar("noticeText", "_text_adminsNoAccessSetUp");
			$this->page->append("errors", $error->processing());
		}
		
		if ( intval($level) < 1 )
		{
			$error = new Template("stores/small_notice.tpl");
			$error->setLocVar("noticeText", "_text_adminsLowLevelAccess");
			$this->page->append("errors", $error->processing());
		}
		
		
		if ( !$errors )
		{
			$this->page->setVar('content', true);
			$this->page->setVar('setName', $name);
			$this->page->setVar('setEmail', $email);
			$this->page->setVar('setPassword', $password);
			$this->page->setVar('setAccess', $access==""?"---":$access);
			$this->page->setVar('setLevel', $level);
		}
		
		$_SESSION['name'] = $name;
		$_SESSION['email'] = $email;
		$_SESSION['password'] = $password;
		$_SESSION['access'] = $access_array;
		$_SESSION['level'] = $level;
		
		
	}
	
	
	public function CreateAction()
	{
		$this->helper->setMenuActive("admins");
		$this->page->title("_titleAdminsList", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		
		if ( isset(
			$_SESSION['name'],
			$_SESSION['email'],
			$_SESSION['password'],
			$_SESSION['access'],
			$_SESSION['level']
		) )
		{
			$create = ORM::for_table('admins')->create();
			$create->email = $_SESSION['email'];
			$create->name = $_SESSION['name'];
			$create->passw = md5($_SESSION['password']);
			$create->access = implode("|", $_SESSION['access']);
			$create->level = $_SESSION['level'];
			$create->save();
			
			$error = new Template("stores/small_success.tpl");
			$error->setLocVar("successText", "_text_adminsAddedSuccessfully");
			$this->page->append("content", $error->processing());
			
			$this->helper->addBTB('/panel/admins/list', '_OK', BTNCOLOR_GREEN);
			
			unset(
				$_SESSION['name'],
				$_SESSION['email'],
				$_SESSION['password'],
				$_SESSION['access'],
				$_SESSION['level']
			);
		}
		else
		{
			header("Location: /panel/admins/confirm");
		}
	}
	
	
	public function ListAction()
	{
		$this->helper->setMenuActive("admins");
		$this->helper->setSubmenuActive('list');
		$this->page->title("_titleAdminsList", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		
		$this->helper->addBTB("/panel/admins/", "_Back", BTNCOLOR_WHITE);
		$this->helper->addBTB("/panel/admins/add", "_adminsCreate", BTNCOLOR_BLUE);
		
		$this->page->loadTplToVar('content', 'admins/list_page.tpl');
		
		$adm_list = ORM::for_table('admins')->find_array();
		$this->page->setVar('admin_list', $adm_list);
	}
	
	public function EditAction()
	{
		$this->helper->setMenuActive("admins");
		$this->page->title("_titleAdminsEdit", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		$this->helper->addBTB("/panel/admins/list", "_Back", BTNCOLOR_WHITE);
		
		
		
	}
	
	public function DeleteAction()
	{
		$this->helper->setMenuActive("admins");
		$this->page->title("_adminsRemoveIt", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		
		$aid = isset($this->site->arguments[0]) ? $this->site->arguments[0] : -1;
		$a = ORM::for_table('admins')->where('id', $aid)->find_one();
		
		if ( $a && $aid != -1 )
		{
			$this->helper->addBTB("/panel/admins/list", "_Abort", BTNCOLOR_WHITE);
			$this->page->loadTplToVar('content', 'admins/remove.tpl');
			$this->page->setVar('adminName', $a->name . " - " . $a->email);
			$this->page->setVar('adminID', $aid);
		}
		else
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_adminsNotExists");
			$this->page->append("content", $error->processing());
			$this->helper->addBTB("/panel/admins/list", "_Abort", BTNCOLOR_BASE);
		}
	}
	
	public function RemoveAction()
	{
		$this->helper->setMenuActive("admins");
		$this->page->title("_adminsRemoveIt", true);
		$this->page->loadTplToVar("body", "admins/base.tpl");
		
		// -------------------------------------------------------------
		
		$aid = $this->site->_post('id') ?: -1;
		$a = ORM::for_table('admins')->where('id', $aid)->find_one();
		if ( $aid != -1 && $a )
		{
			$a->delete();
			$this->helper->addBTB("/panel/admins/list", "_OK", BTNCOLOR_GREEN);
			$error = new Template("stores/small_success.tpl");
			$error->setLocVar("successText", "_text_adminsRemovedSuccess");
			$this->page->append("content", $error->processing());
		}
		else
		{
			$error = new Template("stores/small_error.tpl");
			$error->setLocVar("errorText", "_error_adminsDeleteError");
			$this->page->append("content", $error->processing());
			$this->helper->addBTB("/panel/admins/list", "_Abort", BTNCOLOR_BASE);
		}
	}
}